﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Fitment.Admin
{
    public partial class materiaMng : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindList();
            }
            if (Session["LoginUser"] == null)    //上面Session名字用LoginUser,这里的名字要和上面的相同,可以随便改,保持一致就行了,注意:Session["LoginUser"]后面,没有ToString()哦,你要是加上的话,就会出错了.
            {
                Response.Redirect("logon.aspx"); //如果Session为空,则转到登陆页.
            }
        }

        private string mMaterial = null;//材料名称
        private int mNumPrice, mAreaPrice;  //按数量、面积分类的价格，按最低价格
        private string mRoom = null;  //对应房间类型
        private string mType = null;   //对应工种
        private string mProject=null; //对应工程名称
        private int mDays;      //对应工程时间       
        DS data = new DS();   //连接数据库
        
        protected void searchButton_Click(object sender, EventArgs e)
        {
            bindList();
        }

        protected void query_Click(object sender, EventArgs e)
        {
            mMaterial = this.material.Text.Trim();
            string strSql = "SELECT *FROM Material WHERE MatName='"+mMaterial+"' ";
            bindList(strSql);  //重新绑定控件
        }

        protected void addButton_Click(object sender, EventArgs e)
        {
            mMaterial = this.matName.Text.Trim();
            mProject = this.Project.Text.ToString().Trim();   //获取工程名称
            mDays = Convert.ToInt32(this.ProTime.Text.ToString().Trim());    //获取需要时间
            mNumPrice = Convert.ToInt32(this.numPrice.Text.ToString().Trim());  //按每数量最低价
            mAreaPrice = Convert.ToInt32(this.areaPrice.Text.ToString().Trim());   //按面积最低价
            mRoom = this.roomType.SelectedValue.Trim();
            mType = this.type.SelectedValue.Trim();
            if (mMaterial==""||mRoom==""||mType=="'")
            {
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "提示", "<script>alert('请填写完整信息！');</script>");
                return;
            }

            string strSql = "SELECT *FROM Material WHERE MatName='"+matName+"'";
            DataTable table = data.GetDataSource(strSql, "Query");
            if (table.Rows.Count!=0)
            {
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "提示", "<script>alert('该材料已经存在，请检查！');</script>");
                return;
            }
            else
            {
                strSql = "INSERT INTO Material(WorkType,MatName,Price_Area,Price_Num,MatchType,NeedTime,ProName )Values('" + mType + "','" +
                mMaterial + "','" + mAreaPrice + "','" + mNumPrice + "','" + mRoom + "','"+mDays+"','"+mProject+"')";
                //执行添加记录
                if (data.ExcuteSql(strSql))
                {
                    bindList();
                    
                }
            
            }
            
        }

        protected void bindList(string sql)
        {
            DataTable table = data.GetDataSource(sql, "Query");
            this.GridView1.DataSource = table.DefaultView;
            this.GridView1.DataBind();
            clear();

        }

        protected void bindList()
        {

            string strSql = "SELECT *FROM Material";
            DataTable table = data.GetDataSource(strSql, "Query");
            this.GridView1.DataSource = table.DefaultView;
            this.GridView1.DataBind();   //将数据绑定到控件中
            clear();
        }

        protected void clear()
        {
            this.matName.Text = "";
            this.material.Text = "";
            

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            bindList();  //重新绑定主键
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            mMaterial = (this.GridView1.Rows[e.RowIndex].FindControl("name") as Label).Text.ToString();
            string strSql = "DELETE FROM Material WHERE MatName='"+mMaterial+"'";
            if (data.ExcuteSql(strSql))
            {
                bindList();
                return;
            }
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            mMaterial = (this.GridView1.Rows[e.RowIndex].FindControl("name") as Label).Text.Trim();
            mDays = Convert.ToInt32((this.GridView1.Rows[e.RowIndex].FindControl("days") as TextBox).Text.Trim());
            mProject = (this.GridView1.Rows[e.RowIndex].FindControl("project") as TextBox).Text.Trim();
            mNumPrice = Convert.ToInt32((this.GridView1.Rows[e.RowIndex].FindControl("numPrice") as TextBox).Text.Trim());
            mAreaPrice = Convert.ToInt32((this.GridView1.Rows[e.RowIndex].FindControl("areaPrice") as TextBox).Text.Trim());
            mRoom = (this.GridView1.Rows[e.RowIndex].FindControl("roomType") as DropDownList).Text.Trim();
            mType = (this.GridView1.Rows[e.RowIndex].FindControl("type") as DropDownList).Text.Trim();
            string strSql = "UPDATE Material SET WorkType='"+mType+"',Price_Area='"+mAreaPrice+"',Price_Num='"
                +mNumPrice+"',MatchType='"+mRoom+"',NeedTime='"+mDays+"',ProName='"+mProject+"' WHERE MatName='"+mMaterial+"'" ;
            if (data.ExcuteSql(strSql))
            {
                this.GridView1.EditIndex = -1;
                bindList();
            }
            else
                return;
            
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.GridView1.EditIndex = e.NewEditIndex;
            bindList();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.GridView1.EditIndex = -1;
            bindList();
        }
    }
}
